var tableElem = "LAY-home-jobdepartment-list";
var tableToolsElem = "LAY-home-jobdepartment-tools";
var formElem = "LAY-home-jobdepartment-listform";
var formSubmitElem = "LAY-home-jobdepartment-listform-submit";
var formUrl = '/home/jobdepartment/listform'
var historyUrl = '/home/jobdepartment/list'
var baseUrl = window.globalConfig.url_jobDepartment


layui.use(['common'], function (common) {
    var $ = layui.$
        , admin = layui.admin
        , table = layui.table
        , form = layui.form

    //页面管理
    var tableItem = table.render({
        elem: `#${tableElem}`
        , lineStyle: 'height: 50px;'
        , url: `${baseUrl}`
        , cols: [[
            {type: 'checkbox', fixed: 'left'}
            , {field: 'department_id', width: 100, title: 'id', sort: true}
            , {field: 'department_name', title: '招聘岗位名称', minWidth: 150, edit: 'text'}
            , {field: 'sort_order', title: '排序', edit: 'text'}
            , {title: '操作', minWidth: 220, align: 'center', fixed: 'right', toolbar: `#${tableToolsElem}`}
        ]]
        , page: true
        , limit: 10
        , limits: [10, 15, 20, 25, 30]
        , text: {none: '暂无数据！'}
        , done() {
        }
    });

    var active = {
        //添加
        add: function (othis) {
            location.hash = `${formUrl}`;
        }
    };

    $('.layui-btn.layuiadmin-btn-list').on('click', function () {
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });


    // 增删改查
    table.on(`tool(${tableElem})`, function (obj) {
        var data = obj.data;
        if (obj.event === "detail") {  // 查看详情


        } else if (obj.event === 'del') { // 删除详情
            layer.confirm('确定删除此招聘岗位？', function (index) {
                admin.req({
                    url: baseUrl + "/" + data.id,
                    type: "DELETE",
                    success: function (res) {
                        common.ctmMsg(res.code, res.msg)
                        layer.close(index);
                        tableItem.reloadData(); //重载表格
                    }
                })
            });
        } else if (obj.event === 'edit') {// 修改详情
            location.hash = `${formUrl}/id=` + data.department_id;
        } else if (obj.event === 'copy') {
            layer.confirm('确定复制此岗位？', function (index) {
                var field = data
                field.name = field.name + "_copy"
                delete field.id
                admin.req({
                    url: baseUrl,
                    type: "POST",
                    data: field,
                    success: function (res) {
                        common.ctmMsg(res.code, res.msg)
                        if (res.code === 0) {
                            tableItem.reloadData()
                        }
                    }
                })
            });

        }
    });

    // 表单模块
    layui.data.done = function (d) {
        var remoteData = d.data
        layui.use(['form'], function () {

            form.render(null, formElem); //渲染该模板下的动态表单
            // 加载上传插件
            // common.uploader('ID-upload-jobdepartment-btn', 'ID-upload-jobdepartment-img', 'photo')


            // 加载编辑器
            // let editors = {
            //     'editor': 'content',
            // }
            // common.loadEditors(remoteData,editors)


            // 表单提交
            form.on(`submit(${formSubmitElem})`, function (data) {
                var field = data.field; //获取提交的字段
                field.is_show = field.is_show === undefined ? 0 : field.is_show;
                // field = common.getEditorsContent(field, editors)


                let url = baseUrl
                let type = "POST";
                if (remoteData.id) {
                    url += "/" + remoteData.id;
                    type = "PUT"
                }
                //提交 Ajax 成功后，关闭当前弹层并重载表格
                admin.req({
                    url: url,
                    type: type,
                    data: field,
                    success: function (res) {
                        if (res.code === 0) {
                            setTimeout(() => {
                                history.back()
                            }, 1000)
                        }
                        common.ctmMsg(res.code, res.msg)
                    }
                })

            });
        });
    };

    // 表格文本修改
    common.tableTextEditor(tableElem, baseUrl, 'department_id')
    common.tableSwitchEditor('jobdepartment-is_show-status', baseUrl)

})